package com.ssm.sku.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SkuController {

    @GetMapping("test")
    public String test() {
        return "hello Sku world";
    }

    @GetMapping("timeOut")
    public String timeOut() throws InterruptedException {
        Thread.sleep(3000L);
        return "hello Sku world time Out";
    }

    @GetMapping("testHystrix")
    public String testHystrix() {
        return "hello hystrix";
    }

    /**
     * @HystrixCommand 声明该方法为是一个hystrix命令
     * fallbackMethod属性提供回退方法， 当方法执行失败时调用(如超时、异常等)
     * commandProperties属性可指定Hystrix命令的多个配置属性
     * @HystrixProperty 每个配置都通过 @HystrixProperty 注解来定义，其中 name 属性指定了配置的名称，value 属性指定了配置的值
     * execution.isolation.thread.timeoutInMilliseconds 隔离线程的超时，单位为毫秒。超过指定时间会调用回退方法
     */
    @GetMapping("testHystrixTimeOut")
    @HystrixCommand(fallbackMethod = "testHystrixTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String testHystrixTimeOut() throws InterruptedException {
        Thread.sleep(5000);
        return "Hello hystrix timeOut!";
    }

    public String testHystrixTimeoutHandler() {
        return "我降级了！！！";
    }
}
